package com.sky.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sky.domain.User;
import com.sky.service.UserService;
import com.sky.utils.PassWordutil;

public class SearchUserServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数，搜索的关键字
		String keyString = request.getParameter("key");
		//2.根据关键字(用户名)去数据库表中去查询用户
		UserService userService =new UserService();
		try {
			List<User> list = userService.queryUserByKey(keyString);
			//==========================
			
			List<String> pwd=new ArrayList<String>();
			for(int i=0;i<list.size();i++){
				try {
					PassWordutil des1 = new PassWordutil();
					pwd.add(des1.decrypt(list.get(i).getPassword()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//===========================
			
			
			//把查询结果在用户列表进行显示
			request.setAttribute("list", list);
			request.getRequestDispatcher("home/showuserlist.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("error_search_msg", e.getMessage());
			request.getRequestDispatcher("home/showuserlist.jsp").forward(request, response);
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}


}
